package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.event.NewProblemsEvent;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.question.NewProblemsFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.UdaUtils;

/**
 * 类说明：填写问题
 * Created by Administrator on 2016/9/1.
 */
public class NewProblemsActivity extends AbsTitleBarActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, NewProblemsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //        EventBusUtil.unregisterEvent(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();
            if(count == 0) {
                showConfirm();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    private void showConfirm() {
        saveDraftBox();
    }

    private void saveDraftBox() {
        EventBusUtil.post(new NewProblemsEvent(NewProblemsEvent.EVENT_COMMENT_SAVE));
    }
    @Override
    protected String getTopTitle() {
        return getString(R.string.new_problems);
    }
    @Override
    protected BaseFragment onCreateContentFragment() {
        setIsRequest(false);
        return new NewProblemsFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        EventBusUtil.registerEvent(this);
    }
    @Override
    protected void setupTitleView() {
        setOperateTitle(getString(R.string.user_post_solo));
    }
    @Override
    public boolean shouldShowBack() {
        return false;
    }
    @Override
    protected boolean shouldShowQuestionBack() {
        return true;
    }
    @Override
    public void onHeadBackClick(View view) {
        //        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.READ_ALL_MESSAGE));
        UdaUtils.hideInputMethod(this, view);
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if(count == 0) {
            showConfirm();
        } else {
            try {
                manager.popBackStackImmediate();
            } catch(IllegalStateException ignored) {
            }
        }
    }
    @Override
    public void onHeadActionClick(View view) {
        //        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.READ_ALL_MESSAGE));
        EventBusUtil.post(new NewProblemsEvent(NewProblemsEvent.EVENT_COMMENT_PUBLISHED));
    }

}
